package com.ycsj.common.view.dialog;


import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 自定义的通用Dialog
 * Created by Administrator on 2018/4/13 0013.
 */

public class CustomDialog extends Dialog {

    private Context context;
    private int height, width;
    private boolean cancelTouchout;
    private View view;

    private CustomDialog(Builder builder) {
        super(builder.context);
        context = builder.context;
        height = builder.height;
        width = builder.width;
        cancelTouchout = builder.cancelTouchout;
        view = builder.view;
    }


    private CustomDialog(Builder builder, int resStyle) {
        super(builder.context, resStyle);
        context = builder.context;
        height = builder.height;
        width = builder.width;
        cancelTouchout = builder.cancelTouchout;
        view = builder.view;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(view);

        setCanceledOnTouchOutside(cancelTouchout);

        Window win = getWindow();
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.gravity = Gravity.CENTER;
        lp.height = height;
        lp.width = width;
        win.setAttributes(lp);
    }

    /**
     * 构建者模式
     */
    public static final class Builder {

        private Context context;
        private int height, width;
        private boolean cancelTouchout;
        private View view;
        private int resStyle = -1;


        public Builder(Context context) {
            this.context = context;
        }

        public Builder view(int resView) {
            view = LayoutInflater.from(context).inflate(resView, null);
            return this;
        }

        public Builder heightpx(int val) {
            height = val;
            return this;
        }

        public Builder widthpx(int val) {
            width = val;
            return this;
        }

        public Builder heightdp(int val) {
            height = dp2px(val);
            return this;
        }

        public Builder widthdp(int val) {
            width = dp2px(val);
            return this;
        }

        public Builder heightDimenRes(int dimenRes) {
            height = context.getResources().getDimensionPixelOffset(dimenRes);
            return this;
        }

        public Builder widthDimenRes(int dimenRes) {
            width = context.getResources().getDimensionPixelOffset(dimenRes);
            return this;
        }

        //设置样式
        public Builder style(int resStyle) {
            this.resStyle = resStyle;
            return this;
        }

        //点击外部是否消失
        public Builder cancelTouchout(boolean val) {
            cancelTouchout = val;
            return this;
        }

        //添加监听
        public Builder addViewOnclick(int viewRes, View.OnClickListener listener) {
            view.findViewById(viewRes).setOnClickListener(listener);
            return this;
        }

        //设置文字
        public Builder setText(int viewRes, String message) {
            View v = view.findViewById(viewRes);
            if (v instanceof TextView) {
                ((TextView) v).setText(message);
            }
            return this;
        }

        //设置图片
        public Builder setImage(int viewRes, int imgId) {
            View v = view.findViewById(viewRes);
            if (v instanceof ImageView) {
                ((ImageView) v).setImageResource(imgId);
            }
            return this;
        }

        public CustomDialog build() {
            if (resStyle != -1) {
                return new CustomDialog(this, resStyle);
            } else {
                return new CustomDialog(this);
            }
        }


        /**
         * dp2px的方法
         *
         * @param dpVal
         * @return
         */
        private int dp2px(float dpVal) {

            return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,

                    dpVal, context.getResources().getDisplayMetrics());

        }

    }

}
